package com.example.myapplication.view_type;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.bean.Person;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ListAdapter extends BaseAdapter {
    private List<Person> listBean;
    private Context context;

    private LayoutInflater layoutInflater;

    public ListAdapter(List<Person> listBean, Context context) {
        this.listBean = listBean;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listBean == null ? 0 : listBean.size();
    }

    @Override
    public Object getItem(int i) {
        return listBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View v, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (v == null) {
            // 根据布局文件activity0_1_main_list_content.xml生成转换视图对象
            //v = layoutInflater.inflate(R.layout.activity0_1_main_list_content, null);
            v = View.inflate(context, R.layout.activity0_1_main_list_content, null);
            viewHolder = new ViewHolder(v);
            v.setTag(viewHolder);
        } else {
            viewHolder = ((ViewHolder) v.getTag());
        }
        Person person = (Person) getItem(i);
        viewHolder.tvName.setText(person.getName());
        viewHolder.ivIcon.setImageResource(person.getSex() == 1 ? R.drawable.man_icon : R.drawable.woman_icon);
        return v;
    }

    static class ViewHolder {
        private TextView tvName;
        private CircleImageView ivIcon;

        public ViewHolder(View view) {
            tvName = view.findViewById(R.id.home_list_name);
            ivIcon = view.findViewById(R.id.home_list_icon);
        }
    }
}
